package com.logisticsmanagement.services;

import java.util.ArrayList;
import java.util.List;

import com.logisticsmanagement.dao.CityDao;
import com.logisticsmanagement.dao.DeliveryStationDao;
import com.logisticsmanagement.dao.IndentDao;
import com.logisticsmanagement.dao.RouteDao;
import com.logisticsmanagement.dao.TruckDao;
import com.logisticsmanagement.dao.UserDao;
import com.logisticsmanagement.domain.City;
import com.logisticsmanagement.domain.DeliveryStation;
import com.logisticsmanagement.domain.Indent;
import com.logisticsmanagement.domain.Truck;
import com.logisticsmanagement.domain.User;
import com.logisticsmanagement.vo.BaseResponse;
import com.logisticsmanagement.vo.VoDeliveryStation;
import com.logisticsmanagement.vo.VoIndent;
import com.logisticsmanagement.utils.*;

import static com.logisticsmanagement.utils.GlobelValue.*;

public class IndentService {
	private IndentDao indentDao=new IndentDao();
	private TruckDao truckDao=new TruckDao();
	private CityDao cityDao=new CityDao();
	private DeliveryStationDao deliveryStationDao=new DeliveryStationDao();
	private UserDao userDao=new UserDao();
	private RouteDao routeDao=new RouteDao();

	
	private City city=new City();
	private User user=new User();
	private DeliveryStation deliveryStation=new DeliveryStation();
	
	/**
	 *return true if success to update indents an indent to database
	 */
	public boolean driverUpdateIndents(Truck truck){
		//to modify indent state such as currentCity
		//jundge whether driver is legal. 
		boolean flag=false;
		Indent newIndent=new Indent();
		newIndent.setTruckID(truck.getTruckID());
		
		List<Indent> indentList=new ArrayList<Indent>();
		indentList=indentDao.selectPage(newIndent, 1, PAGE_SIZE);
		for(int i=0;i<indentList.size();i++){
			//set indent attribute and save into database.
			indentList.get(i).setState(STATE_STATION_CITY);
			indentList.get(i).setCurrentCityID(truck.getFromCityID());
			flag=indentDao.update(indentList.get(i));
		}
		return flag;

	}
		
	public boolean addIndent(Indent newIndent){
		//set prepareDelete and isDelete attribute  as false		
		newIndent.setPrepareDelete(false);
		newIndent.setIsDelete(false);
		DeliveryStation startDeliveryStation=new DeliveryStation();
		DeliveryStation endDeliveryStation=new DeliveryStation();
		Integer startCityID=null;
		Integer endCityID=null;
		
		//get startCityID
		user=userDao.selectByID(newIndent.getSenderID());
		startDeliveryStation=deliveryStationDao.selectById(user.getDeliveryStationID());
		startCityID=startDeliveryStation.getCityID();
		
		
		
		//get endCityID
		endDeliveryStation=deliveryStationDao.selectById(newIndent.getReceiverDeliveryStationID());
		endCityID=endDeliveryStation.getCityID();
		

		newIndent.setGoodTotalPrice(this.getTotalPrice(startCityID, 
				endCityID, newIndent.getGoodWeight()));
		
		return indentDao.add(newIndent);
	}
	
	/**
	 * jundge whether indent is existed in database through orderID 
	 * return VoIntent if exists ;
	 * @return
	 */
	public BaseResponse<VoIndent> queryIndentByID(int orderID){		
		VoIndent voIndent=new VoIndent();
		Indent queryIndent=new Indent();
		Truck truck=new Truck();
		
		List<VoIndent> listVoIndent=new ArrayList<VoIndent>();
		//set default value
		BaseResponse<VoIndent> baseResponseVoIndent =new BaseResponse<VoIndent>(QUERY_NOT_FOUND,
				QUERY_NOT_FOUND_DESCRIPTON,listVoIndent);
		
		if((queryIndent=indentDao.selectById(orderID))!=null){
			voIndent.setOrderID(queryIndent.getOrderID());
			voIndent.setTruckID(queryIndent.getTruckID());
			
			//through truckID to get carName to ensure this good is delievring.
			truck=truckDao.selectByID(queryIndent.getTruckID());
			voIndent.setCarName(truck.getCarNumber());
			//By currentCityID to get currentCityID and currentCityName 
			city=cityDao.selectById(queryIndent.getCurrentCityID());
			voIndent.setCurrentCityID(city.getCityID());
			voIndent.setCurrentCityName(city.getName());
			
			//By receiverDeliveryStationID to get receiverDeliveryStationName
			deliveryStation=deliveryStationDao.selectById(
					queryIndent.getReceiverDeliveryStationID());
			voIndent.setReceiverDeliveryStationID(deliveryStation.getDeliveryStationID());
			voIndent.setReceiverDeliveryStationName(deliveryStation.getStationName());
			//set indent state
			voIndent.setState(queryIndent.getState());
			voIndent.setReceiverName(queryIndent.getReceiverName());
			voIndent.setReceiverPoneNumber(queryIndent.getReceiverPhoneNumber());
			//get sendTimeStamp
			voIndent.setSendTimeStamp(queryIndent.getCreateTimeStamp());
			voIndent.setGoodName(queryIndent.getGoodName());

			listVoIndent.add(voIndent);
			//et other parameter here.
			baseResponseVoIndent.setResponseCode(QUERY_SUCCESS);
			baseResponseVoIndent.setResponseDescription(QUERY_SUCCESS_DESCRIPTON);
			return baseResponseVoIndent;
		}else{
			return baseResponseVoIndent;
		}
	}
	

	/**
	 * this methods to calculate how much client needs to pay
	 * @param startCityID
	 * @param endCityID
	 * @param weight
	 * @return
	 */
	private double getTotalPrice(int startCityID,int endCityID,double weight){
		//get totalPrice per KG from start to end .
		double totalPricePerKG=(routeDao.selectById(startCityID, endCityID)).getPricePerKG();
		
		//get totalPrice 
		double totalPrice=totalPricePerKG*weight*PRICE_PROPORTION;
		return totalPrice;
	}

}
